<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2019/1/14
 * Time: 10:42
 */

namespace app\api\validate;


use app\lib\exception\ParameterException;

class LabelParameter extends BaseValidate
{
    protected $rule=[
        'user_id'=>'require|isPositiveInt',
        'del'=>'checkDel',
        'add'=>'checkAdd',
        'label'=>'checkValue',
    ];

    protected $message=[
        'user_id'=>'用户ID必须为正整数',
    ];

    /**
     * 检查label
     * @param $value
     * @return bool
     * @throws ParameterException
     */
    public function checkValue($value){
        if (empty($value)){
           return true;
        }
        if (is_array($value)){
            return true;
        }else{
            throw new ParameterException([
                'msg'=>'参数不正确'
            ]);
        }
    }

    /**
     * 删除参数验证规则
     * @param $value
     * @return bool
     * @throws ParameterException
     */
    public function checkDel($value){
        if (!empty($value)){
            if (is_array($value)){
                foreach ($value as $item) {
                    $this->checkParams($item);
                }
            }else{
                throw new ParameterException([
                    'msg'=>'参数格式不正确'
                ]);
            }
        }
        return true;
    }

    /**
     * 添加参数验证规则
     * @param $value
     * @return bool
     * @throws ParameterException
     */
    public function checkAdd($value){
        if (!empty($value)) {
            if (is_array($value)) {
                foreach ($value as $item) {
                    $this->checkParams($item);
                }
            } else {
                throw new ParameterException([
                    'msg' => '参数格式不正确'
                ]);
            }
        }
        return true;
    }

    /**
     * 正整数检查
     * @param $value
     * @return bool
     * @throws ParameterException
     */
    private function checkParams($value){
        if (is_numeric($value) && is_int($value+0) && ($value + 0)>0 ){
            return true;
        }else{
            throw new ParameterException([
                'msg'=>'ID必须为正整数'
            ]);
        }
    }
}